<?php

namespace App\Exports;

use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\Specification;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithStyles;
//单元格格式化,单元格处理文本、数字、日期、金额等格式
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
//自动适应单元格宽
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ProductExport implements FromCollection, WithColumnFormatting, ShouldAutoSize,WithStyles
{
    private $params;
    private $job_type_num = [];
    private $specification_num = [];
    private $model_number_num = [];
    /**
     * @var string[] 导出标题集合
     */
    private $headingList = [
        ['产品列表','','','',''],
        ['','','','',''],
        ['ID'=>'ID',
            'name'=>'产品名称',
            'model_number'=>'型号',
            'specification'=>'规格',
            'job_type'=>'工种'
        ]
    ];
    public function __construct($params) {
        ob_end_clean();
        $this->params = $params;
    }

    //数组转集合
    public function collection()
    {
        return new Collection($this->createData());
    }

    //业务代码
    public function createData()
    {
        $data[] = $this->headingList;
        $params = $this->params;
        $list = Product::Search($params)
            ->with(['job_type','model_number'])
            ->orderBy('id','desc')->get()->toArray();
        //p($list);
        $count = [];
        foreach ($list as $key=>&$val){
            if(!empty($val['model_number'])){
                $model_number_count = 0;
                foreach($val['model_number'] as $k=>$v){
                    $specification_id_arr = ModelNumberSpecification::where('product_id',$val['id'])
                        ->where('model_number_id',$v['id'])
                        ->pluck('specification_id')
                        ->toArray();
                    if(!empty($specification_id_arr)){
                        $specification_count = count($specification_id_arr) * count($val['job_type']);
                        $this->specification_num[] =$specification_count;
                        $specification_list = Specification::whereIn('id',$specification_id_arr)
                            ->where('is_del',2)
                            ->get()->toArray();
                        if(!empty($specification_list)){
                            foreach($specification_list as $kk=>$vv){
                                if(!empty($val['job_type'])){
                                    $this->job_type_num[] = count($val['job_type']);
                                    foreach($val['job_type'] as $kkk=>$vvv){
                                        $data[] = [
                                            'id'=>$val['id']??'',
                                            'name'=>$val['product_name']??'',
                                            'model_number'=>$v['model_number']??'',
                                            'specification'=>$vv['specification']??'',
                                            'job_type'=>$vvv['job_type']??''
                                        ];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return $data;
    }



    public function columnFormats(): array
    {
        //'B' => NumberFormat::FORMAT_DATE_DDMMYYYY, //日期
        $format = NumberFormat::FORMAT_NUMBER_00;//金额保留两位小数
        return [];

    }


    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->title;
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 100, 'B' => 100, 'C' => 100, 'D' => 100, 'E' => 100
        ];
    }




    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
        $sheet->mergeCells('A1:E2'); //合并单元格
        $sheet->getStyle('A1:E2')
            ->applyFromArray([
                'font' => ['bold' => true],
                'color' => ['rgb' => '0072ff']
            ]);//字体设置
        // 设置背景色
        $sheet->getStyle('A1:E2')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('e9e9e9');

        // setWrapText自动换行，setVertical垂直对齐方式
        $sheet->getStyle("A:E")->getAlignment()->setVertical('center');
        $sheet->getStyle('A:E')->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中
//        $item = 'D';//需要合并的单元格
//        $start = 4;
//        foreach ($this->job_type_num as $key => $value) {
//            $end = $start + $value - 1;// 4  6   7 10
//            $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
//            $start = $end + 1;
//        }
//        $item = 'C';
//        $start = 4;
//        foreach ($this->specification_num as $key => $value) {
//            $end = $start + $value - 1;// 4  6   7 10
//            $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
//            $start = $end + 1;
//        }

    }

}
